import socket
import time
def send_msg(server_udp):
    dest_ip=input('请输入对方的IP:')
    dest_port=int(input('请输入对方的port:'))
    msg = input('>>>>>').encode('utf-8')
    server_udp.sendto(msg, (dest_ip,dest_port))


def recv_msg(server_udp):
    msg, addr = server_udp.recvfrom(1024)
    print('msg:', msg.decode('utf-8'), 'addr:', addr)


def main():
    time.sleep(3)
    server_udp=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
    dset_ip=('127.0.0.1',8000)
    server_udp.bind(dset_ip)

    while True:
    # 1.发送数据
        opt=input('>>>')
        if opt=='1':
            send_msg(server_udp)
    #2.接受并显示
        if opt=='2':
            recv_msg(server_udp)
        if opt=='3':
            break


if __name__ == '__main__':
    main()